package com.kingbar.app.chat.kingwesocket.ws;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.websocket.server.ServerEndpointConfig;

public class SpringConfigurator extends ServerEndpointConfig.Configurator {

    private static WebApplicationContext context;

    public static void setApplicationContext(WebApplicationContext ctx) {
        context = ctx;
    }

    @Override
    public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
        if (context == null) {
            throw new IllegalStateException("Spring ApplicationContext is not initialized.");
        }
        return context.getAutowireCapableBeanFactory().createBean(endpointClass);
    }
}
